﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Util;
using EFCore.Sharding;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Quartz.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers
{
    [Route("/[controller]/[action]")]
    [AllowAnonymous]
    public class TestController : BaseController
    {
        readonly IDbAccessor _repository;
        readonly IConfiguration _configuration;
        readonly ILogger<TestController> _logger;
        readonly IBase_InterfaceLogBusiness _base_InerfaceLog;
        public TestController(IDbAccessor repository, IConfiguration configuration, ILogger<TestController> logger, IBase_InterfaceLogBusiness base_InerfaceLog) : base(configuration)
        {
            _repository = repository;
            _configuration = configuration;
            _logger = logger;
            //_logger.Log(LogLevel.Error, "143534534");
            _logger.LogInformation("112324");
            _logger.LogWarning("sdfwefw");
            _logger.LogError("sdfwefw");
            _logger.LogCritical("sdfsdff");
            _base_InerfaceLog = base_InerfaceLog;
        }

        [HttpPost]
        public async Task<string> GetMsg(string str)
        {
            Base_InterfaceLog base_InterfaceLog = new Base_InterfaceLog();
            return $"我是回传消息";
        }


        [HttpPost]
        public async Task PressTest()
        {
            Base_User base_User = new Base_User
            {
                Id = Guid.NewGuid().ToString(),
                Birthday = DateTime.Now,
                CreateTime = DateTime.Now,
                CreatorId = Guid.NewGuid().ToString(),
                DepartmentId = Guid.NewGuid().ToString(),
                Password = Guid.NewGuid().ToString(),
                RealName = Guid.NewGuid().ToString(),
                Sex = Sex.Man,
                UserName = Guid.NewGuid().ToString()
            };

            await _repository.InsertAsync(base_User);
            await _repository.UpdateAsync(base_User);
            await _repository.GetIQueryable<Base_User>().Where(x => x.Id == base_User.Id).FirstOrDefaultAsync();
            await _repository.DeleteAsync(base_User);
        }

        [HttpGet]
        public async Task<PageResult<Base_UserLog>> GetLogList()
        {
            return await _repository.GetIQueryable<Base_UserLog>().GetPageResultAsync(new PageInput());
        }

        [HttpPost]
        public ActionResult GetFileStreamTest()
        {

            //_logger.LogInformation("112324");

            //int flag = await _repository.UpdateAsync<Base_User>(u => u.Id == "Admin", u => { u.UserName = "WW"; });
            //if (flag == 0)
            //{
            //    return "无更新数据";
            //}
            //else
            //{
            //    return "更新成功";
            //}

            // 假设你有一个文件路径
            string filePath = @"C:\Users\lenovo\Desktop\5、员工离职移交表.docx";

            // 确保文件存在
            if (!System.IO.File.Exists(filePath))
            {
                //throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            // 创建HttpResponseMessage实例
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

            // 打开文件流
            var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            // 设置返回内容头
            //result.Content = new StreamContent(stream);
            //result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            //result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            //{
            //    FileName = "file.txt"
            //};

            // 返回结果
            return File(stream, "application/octet-stream", "5、员工离职移交表.docx");

        }
    }
}